<?php
/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

namespace Al\Core\Lib;

/**
 * The event service.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 */
class Event {

  /**
   * @var array A collection of handlers.
   */
  protected $handler = array();

  /**
   * Assigns an event listener.
   *
   * @param string $name
   * @param callable $fn
   * @param bool $single [optional] If true can only be fired once.
   * @return $this
   */
  public function on($name, $fn, $single = false) {
    if (!is_callable($fn)) {
      throw new \InvalidArgumentException('An event handler must be callable');
    }
    if (isset($this->handler[$name])) {
      foreach ($this->handler[$name] as &$handler) {
        if ($handler['fn'] === $fn) return $this;
      }
      $this->handler[$name][] = array('fn' => $fn, 'single' => $single);
    } else {
      $this->handler[$name] = array(array('fn' => $fn, 'single' => $single));
    }
    return $this;
  }

  /**
   * Unassigns an event listener.
   *
   * @param string $name [optional]
   * @param callable $fn [optional]
   * @return $this
   */
  public function un($name = null, $fn = null) {
    if ($name) {
      if ($fn) {
        if (!isset($this->handler[$name])) return $this;
        foreach ($this->handler[$name] as $key => &$handler) {
          if ($handler['fn'] === $fn) unset($this->handler[$name][$key]);
        }
      } else {
        $this->handler[$name] = array();
      }
    } else {
      $this->handler = array();
    }
    return $this;
  }

  /**
   * Fires all assigned event listeners.
   *
   * @param string $name
   * @param array $args [optional]
   * @return bool
   */
  public function fire($name, $args = array()) {
    if (!isset($this->handler[$name])) return;
    foreach ($this->handler[$name] as $key => &$handler) {
      $result = call_user_func_array($handler['fn'], $args);
      if ($handler['single']) unset($this->handler[$name][$key]);
      if (is_bool($result)) return $result;
    }
  }

}